package com.example.seakdreamandroid.lottery;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.seakdreamandroid.R;

import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class LuckDrawView extends FrameLayout {
    private PrizeItemView itemView1, itemView2, itemView3,
            itemView8, itemView4,
            itemView7, itemView6, itemView5;
    private TextView ivStart;
    private OnItemListener listener;

    private PrizeItemApi[] itemViewArr = new PrizeItemApi[8];

    private int currentIndex = 0;
    private int currentTotal = 0;
    private int stayIndex = 0;

    private boolean isMarqueeRunning = false;//
    private boolean isGameRunning = false; //是否处于转动
    private boolean isTryToStop = false;//是否增速/减速

    private static final int DEFAULT_SPEED = 150; //默认/最慢速度
    private static final int MIN_SPEED = 50;//最快速度
    private int currentSpeed = DEFAULT_SPEED;//当前速度

    Timer timer = new Timer();


    public LuckDrawView(@NonNull Context context) {
        this(context, null);
    }

    public LuckDrawView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LuckDrawView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        inflate(context, R.layout.lottery_luckdrawview, this);
        initView();
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        startMarquee();
    }

    @Override
    protected void onDetachedFromWindow() {
        stopMarquee();
        super.onDetachedFromWindow();
    }


    private void initView() {
        itemView1 = (PrizeItemView) findViewById(R.id.item1);
        itemView2 = (PrizeItemView) findViewById(R.id.item2);
        itemView3 = (PrizeItemView) findViewById(R.id.item3);
        itemView4 = (PrizeItemView) findViewById(R.id.item4);
        itemView5 = (PrizeItemView) findViewById(R.id.item5);
        itemView6 = (PrizeItemView) findViewById(R.id.item6);
        itemView7 = (PrizeItemView) findViewById(R.id.item7);
        itemView8 = (PrizeItemView) findViewById(R.id.item8);

        itemViewArr[0] = itemView1;
        itemViewArr[1] = itemView2;
        itemViewArr[2] = itemView3;
        itemViewArr[3] = itemView4;
        itemViewArr[4] = itemView5;
        itemViewArr[5] = itemView6;
        itemViewArr[6] = itemView7;
        itemViewArr[7] = itemView8;


        ivStart = findViewById(R.id.iv_start);
        ivStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick();
            }
        });
    }

    private void stopMarquee() {
        isMarqueeRunning = false;
        isGameRunning = false;
        isTryToStop = false;
    }


    private void startMarquee() {
        isMarqueeRunning = true;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isMarqueeRunning) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
//                    //灯带
//                    post(new Runnable() {
//                        @Override
//                        public void run() {
//                            if (bg_1 != null && bg_2 != null) {
//                                if (VISIBLE == bg_1.getVisibility()) {
//                                    bg_1.setVisibility(GONE);
//                                    bg_2.setVisibility(VISIBLE);
//                                } else {
//                                    bg_1.setVisibility(VISIBLE);
//                                    bg_2.setVisibility(GONE);
//                                }
//                            }
//                        }
//                    });
                }
            }
        }).start();
    }

    //获取sleep时间，即转盘运动速度，越小即越快。
    private long getInterruptTime() {
        currentTotal++;
        if (isTryToStop) {//减速直至恢复为默认速度
            currentSpeed += 10;
            if (currentSpeed > DEFAULT_SPEED) {
                currentSpeed = DEFAULT_SPEED;
            }
        } else {//增速直至最快速度
            //第一圈不做加速
            if (currentTotal / itemViewArr.length > 0) {
                currentSpeed -= 10;
            }
            if (currentSpeed < MIN_SPEED) {
                currentSpeed = MIN_SPEED;
            }
        }
        return currentSpeed;
    }

    //返回当前状态
    public boolean isGameRunning() {
        return isGameRunning;
    }

    //开始抽奖
    public void startGame(final int stayIndex) {
        if (isGameRunning) {//运动中
            return;
        }
        isGameRunning = true;
        isTryToStop = false;
        currentSpeed = DEFAULT_SPEED;
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (isGameRunning) {
                    try {
                        Thread.sleep(getInterruptTime());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    post(new Runnable() {
                        @Override
                        public void run() {
                            //防止线程多走了一次
                            if (!isGameRunning) {
                                return;
                            }
                            int preIndex = currentIndex;
                            currentIndex++;
                            if (currentIndex >= itemViewArr.length) {
                                currentIndex = 0;
                            }

                            //根据前后两个itemView的视图变换，实现转动效果
                            itemViewArr[preIndex].setFocus(true);
                            itemViewArr[currentIndex].setFocus(false);

                            //减速&&速度达到最小&&当前view为指定view——停止
                            if (isTryToStop && currentSpeed == DEFAULT_SPEED && stayIndex == currentIndex) {
                                isGameRunning = false;
                                listener.onShop(currentIndex);
                            }
                        }
                    });
                }
            }
        }).start();

        //五秒后开始减速
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                tryToStop();
            }
        }, 5000);
    }


    //改变加速状态
    public void tryToStop() {
        isTryToStop = true;
    }


    public void setData(List<PrizeListBean> beans) {
        if (beans != null) {
            for (int i = 0; i < beans.size(); i++) {
                itemViewArr[i].setUi(beans.get(i));
            }
        }
    }


    public void setOnItemListener(OnItemListener listener) {
        this.listener = listener;
    }

    //放个接口出来做事件监听
    public interface OnItemListener {
        void onClick();//点击开始

        void onShop(int Index);//转动停止
    }
}
